package com.woniuxy.sale.service.impl;

import com.woniuxy.commons.entity.Dict;
import com.woniuxy.sale.mapper.DictDao;
import com.woniuxy.sale.service.DictService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 数据字典获取
 * <P> Date: 2022/4/19:17:40
 * <P> Description:
 */

@Service
public class DictServiceImpl implements DictService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DictDao dictDao;

    @Override
    @Cacheable(value = "dict", sync = true)
    public Map<Long, Dict> findAllDict() {

        Map<Long, Dict> collect = dictDao.findAll()
                .stream()
                .collect(Collectors
                        .toMap(Dict::getId,
                                dict -> dict));

        redisTemplate.opsForValue().set("dict", collect);

        return collect;
    }
}
